package com.iflytek.uoamp.notify.core.action;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.iflytek.uoamp.common.notify.constant.ConstField;
import com.iflytek.uoamp.common.notify.exception.NotifyException;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.obj.Period;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;
import com.iflytek.uoamp.notify.core.protocol.DirectNotification;


public class FullNotifyAction extends AbstractNotifyAction{

    @Override
    public boolean doTask(Notification item) {
        DirectNotification notification = (DirectNotification)item;
        
        if(!integrityCheck(notification)) {
            return false;
        }
        
        long currentTime = System.currentTimeMillis();
        
        try {
        	List<Period> periods = notification.getAllowTime();
            if(!NotifyUtils.isInPeriods(currentTime, periods)) {
            	String formattedTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
                logInfo.addError(MessageFormat.format(ConstField.PACKAGETIMEISNOTACCEPTED, formattedTime, periods));
                return false;
            }
            
            if(Dispatcher.getInstance(logInfo).dispatch(notification, currentTime)) {
                timeMaps.put(uniqueID, currentTime);
            } else {
                return false;
            }
        } catch(NotifyException e) {
            logInfo.addError(e.getMessage());
            return false;
        }

        return true;
    }
    
}
